package com.fas.hehe.system.controller;


import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;

import com.fas.core.BaseController;
import com.fas.core.plugin.jfinal.route.ControllerBind;
import com.fas.core.plugin.shiro.exception.IncorrectCaptchaException;
import com.fas.core.plugin.shiro.kit.FasShiroKit;
import com.jfinal.aop.Clear;
import com.jfinal.core.ActionKey;
import com.jfinal.ext.render.CaptchaRender;
import com.jfinal.kit.StrKit;
/**
 * 
 * 
 *
 * @Copyright: Copyright (c) 2016 FAS 
 * @anthor yixiaoping<thinker1013@163.com>
 * @since 2016年1月23日 下午8:03:34
 * @version V1.0
 */
@ControllerBind(controllerKey="/login")
public class LoginController extends BaseController {
	
	public void index() {
		String exceptionClassName = this.getAttrForStr("shiroLoginFailure");
		String error = null;
		if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
			error = "用户名/密码错误";
		} else if (IncorrectCredentialsException.class.getName().equals(
				exceptionClassName)) {
			error = "用户名/密码错误";
		} else if(IncorrectCaptchaException.class.getName().equalsIgnoreCase(exceptionClassName)){
			error = "验证码错误";
		} else if(AuthenticationException.class.getName().equals(exceptionClassName)){
			error = "用户名/密码错误";
		} else if(ExcessiveAttemptsException.class.getName().equals(exceptionClassName)){
			error = "错误尝试5次，帐号将锁定10分钟。";
		}else if (StrKit.notBlank(exceptionClassName) ) {
			error = "其他错误：" + exceptionClassName;
		}
		
		  /*就是代表当前的用户。*/

		Subject currentUser = FasShiroKit.getSubject();
        if(currentUser.isAuthenticated()){
			this.redirect("/index");
        }else{
        	this.setAttr("error", error);
			this.render("/page/login.jsp");
        }
        /**
        if(StrKit.notNull(error)){
			this.setAttr("error", error);
			this.render("/page/login.jsp");
		}else{
			this.saveLoginLog();
			this.redirect("/index");
		}**/
	}
	
    // 验证码
    @Clear
    @ActionKey("/login/captcha")
    public void captcha() {
            render(new CaptchaRender("captcha"));
    }

}
